package com.doubi.common.config.transaction;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.util.Properties;

@Configuration
public class TransactionConfig {

    /** 
     * 功能描述：配置事务拦截器
     * @author   MrDoubi 2019-02-15 17:07
     * @param platformTransactionManager        事务管理器，JPA默认为JpaTransactionManager
     * @return                                  返回事务拦截器
     */
    @Bean(name = "txAdvice")
    public TransactionInterceptor transactionInterceptor(PlatformTransactionManager platformTransactionManager) {
        TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
        // 事务管理器
        transactionInterceptor.setTransactionManager(platformTransactionManager);

        Properties transactionAttributes = new Properties();
        //读写
        transactionAttributes.setProperty("save*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("add*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("create*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("insert*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("update*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("merge*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("del*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("remove*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("put*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("use*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("edit*", "PROPAGATION_REQUIRED,-Throwable");
        //只读
        transactionAttributes.setProperty("get*", "PROPAGATION_REQUIRED,-Throwable, readOnly");
        transactionAttributes.setProperty("count*", "PROPAGATION_REQUIRED,-Throwable, readOnly");
        transactionAttributes.setProperty("find*", "PROPAGATION_REQUIRED,-Throwable, readOnly");
        transactionAttributes.setProperty("list*", "PROPAGATION_REQUIRED,-Throwable, readOnly");
        transactionAttributes.setProperty("select*", "PROPAGATION_REQUIRED,-Throwable, readOnly");
        transactionAttributes.setProperty("*", "PROPAGATION_REQUIRED,-Throwable, readOnly");

        transactionInterceptor.setTransactionAttributes(transactionAttributes);
        return transactionInterceptor;
    }

    /**
     * 功能描述：代理到Service和ServiceImpl的Bean
     * @author   MrDoubi 2019-02-15 17:09
     * @return      返回代理
     */
    @Bean
    public BeanNameAutoProxyCreator transactionAutoProxy() {
        BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();
        transactionAutoProxy.setProxyTargetClass(true);
        transactionAutoProxy.setBeanNames("*Service", "*ServiceImpl");
        transactionAutoProxy.setInterceptorNames("txAdvice");
        return transactionAutoProxy;

    }


}
